//
//  UIViewControllerEx.swift
//  Extend
//
//  Created by 元穗 on 2017/5/18.
//  Copyright © 2017年 yuansui. All rights reserved.
//

import UIKit

public typealias OnFormItemClick = () -> ();

open class FormEx {
    var type: Int?;

    var name: String?;
    var hint: String?;
    var text: String?;
    var regex: String?;

    var height: Int?;

    var related: Any?;

    var image: UIImage?;
    var bgColor: UIColor?;

    // 以下是服务器传输字段
    var key: String?;
    var val: String?;

    var onClick: OnFormItemClick?;
    var viewClickMap: Dictionary<Int, OnFormItemClick> = Dictionary();

    
    open func getTableId() -> String? {
        return nil;
    }
    
    open func initItem(_ itemCell : FormCellEx){
        itemCell.delegate = self as? FormCellExDelegate;
    }

    open func refreshItem(_ itemCell : FormCellEx){
      
    }
    public init() {

    }
}


open class FormBuilderEx<T:FormEx> {

    var type: Int = -1;

    var name: String?;
    var hint: String?;
    var text: String?;
    var regex: String?;

    var height: Int?;

    var related: Any?;

    var image: UIImage?;
    var bgColor: UIColor?;

    var onClick: OnFormItemClick?;
    var viewClickMap: Dictionary<Int, OnFormItemClick> = Dictionary();


    public init(_ t: Int) {
        type = t;
    }

    public func name(_ n: String) -> Self {
        name = n;
        return self;
    }

    public func hint(_ h: String) -> Self {
        hint = h;
        return self;
    }

    public func text(_ t: String) -> Self {
        text = t;
        return self;
    }

    public func regex(_ r: String) -> Self {
        regex = r;
        return self;
    }

    public func height(_ h: Int) -> Self {
        height = h;
        return self;
    }

    public func related(_ r: Any) -> Self {
        related = r;
        return self;
    }

    public func image(_ i: UIImage) -> Self {
        image = i;
        return self;
    }

    public func bgColor(_ c: UIColor) -> Self {
        bgColor = c;
        return self;
    }

    public func click(_ click: @escaping OnFormItemClick) -> Self {
        onClick = click;
        return self;
    }

    public func viewClick(_ viewId: Int, _ click: @escaping OnFormItemClick) -> Self {
        viewClickMap.updateValue(click, forKey: viewId);
        return self;
    }


    open func build(_ type: Int) -> T? {
        return nil;
    }

    public func build() -> T? {
        if let f = build(type) {
            f.type = type;

            f.name = name;
            f.hint = hint;
            f.text = text;
            f.regex = regex;

            f.related = related;

            f.image = image;
            f.bgColor = bgColor;

            return f;
        }

        return nil;
    }
   
    
    public func getTableId() -> String? {
        return nil;
    }
    
    public func initItem(_ itemCell : FormCellEx){
        itemCell.delegate = self as? FormCellExDelegate;
    }
    
    public func refreshItem(_ itemCell : FormCellEx){
        
    }

}




